﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StreamServeDSIGenerator.UI.Process.PromptDialog
{
    public partial class importDialog : Form
    {
        public event CancelEventHandler validate;
        public importDialog()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialogImport.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = String.Empty;
                filePath = openFileDialogImport.InitialDirectory + openFileDialogImport.FileName;
                txtFileName.Text = filePath;
                
            }
        }

        private bool onValidate()
        {
            CancelEventHandler handler = validate;

            if (handler == null)
            {
                return true;
            }

            CancelEventArgs args = new CancelEventArgs();
            handler(this, args);
            return args.Cancel;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (onValidate())
            {
                DialogResult = DialogResult.OK;
            }
            //if (!this.ValidateChildren())
            //{
            //    this.DialogResult = System.Windows.Forms.DialogResult.No;
            //}
            //else
            //{
            //    this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //}
            //     MessageBox.Show(this.ValidateChildren().ToString());
            //if (!this.ValidateChildren())
            //{
            //    this.DialogResult = System.Windows.Forms.DialogResult.No;
            //}

        }

        private void txtFileName_Validating(object sender, CancelEventArgs e)
        {
            string fileName = String.Empty;
            fileName = txtFileName.Text.Trim();

            try
            {
                if (String.IsNullOrEmpty(fileName))
                {
                    errorProviderImportDialog.SetError(txtFileName, "Please select a file.");
                 
                }
             
            }
            catch (Exception ex)
            {
                MessageBox.Show(this,"Selected file cannot be open or does not have necessary privilege", "Fail to open file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtBlockStart_Validating(object sender, CancelEventArgs e)
        {
            string strBlockStart = String.Empty;

            strBlockStart = txtBlockStart.Text.Trim();

            if (String.IsNullOrEmpty(strBlockStart))
            {
                e.Cancel = false;
                errorProviderImportDialog.SetError(txtBlockStart, "Please specify a block delimiter.");
            }
           
        }
      
    }
}
